<?php
/**
 * rakuten海外仓(谷仓)连接请求
 */

namespace App\Logic\Plat\Rakuten\Overseas\GuCang;

use App\Models\Basics\SysPlat;

class RakutenOverseasClient{

    //乐天海外仓(谷仓)配置
    const CONTENT_TYPE = "Content-Type: application/json; Accept: application/json; charset=utf-8";
    const API_URL = "https://oms.goodcang.net"; //谷仓api地址
    const APP_TOKEN = "d73c22f24710350598b656ca307b509b"; //谷仓token
    const APP_KEY = "d9985ca4a86de9dd0cb8c3b5830dc7df"; //谷仓秘钥

    //测试
    //const API_URL = "https://uat-oms.eminxing.com"; //谷仓api地址
    //const APP_TOKEN = "7013991264f611e98ea200e01b680258"; //谷仓token
    //const APP_KEY = "6ff50abf64f611e98ea200e01b680258"; //谷仓秘钥

    const platId = SysPlat::PLAT_RAKUTEN; //rakuten平台ID
    const PLATFORM = 'RAKUTEN'; //rakuten平台
    const SHIPPING_METHOD = 'JP_TAK_KYUUBIN'; //物流产品代码
    const WAREHOUSE_CODE = 'JP'; //配送仓库代码(区域仓)
    const WP_CODE = 'JP-3'; //物理仓(日本3号仓)

    /**
     * 获取请求
     * @param $apiUrl
     * @param $apiParams
     * @return mixed
     */
    public static function getRequest($apiUrl,$apiParams){
        $obj = new RakutenOverseasClient();
        $res = $obj->curlPost($apiUrl,$apiParams);
        return json_decode($res);
    }

    /**
     * curl请求(post)
     * @param $url
     * @param $apiParams
     * @return bool|string
     */
    public function curlPost($url,$apiParams){

        $apiUrl = self::API_URL.'/'.$url;
        $ch = curl_init();
        curl_setopt_array($ch, [
            CURLOPT_URL => $apiUrl,
            CURLOPT_SSL_VERIFYPEER => false, //认证证书来源的检查
            CURLOPT_RETURNTRANSFER => true, //设置信息以文件流的形式返回
            CURLOPT_ENCODING => '', //设置头信息字符集
            CURLOPT_MAXREDIRS => 10, //设置请求的最大重定向次数
            CURLOPT_TIMEOUT => 60, //设置请求超时时间
            CURLOPT_CONNECTTIMEOUT => 60, //设置超时限制防止死循环
            CURLOPT_FOLLOWLOCATION => true, //返回的所有重定向信息
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, //设置http版本
            CURLOPT_CUSTOMREQUEST => 'POST', //设置请求方式
            CURLOPT_POSTFIELDS => json_encode($apiParams), //设置请求参数
            CURLOPT_HTTPHEADER => [
                'app-token: '.self::APP_TOKEN,
                'app-key: '.self::APP_KEY,
                self::CONTENT_TYPE
            ],
        ]);
        $result = curl_exec($ch);
        
        curl_close($ch);
        return $result;
    }

    /**
     * 获取所有页数据，组装合并（集合）
     * @param $result
     * @param $apiUrl
     * @param $apiParams
     * @return array
     */
    public static function getResultList($result,$apiUrl,$apiParams){
        $listData = [];
        $pageSazes = intval(ceil($result->count / $apiParams['pageSize'])); //总页码 = 总条数/页面数量
        for ($i = 1; $i <= $pageSazes; $i++){
            $apiParams['page'] = $i; //翻页，下一页(页码)
            $result1 = self::getRequest($apiUrl,$apiParams);
            if ($result1) {
                if ($result1->message == 'Success') {
                    if ($result1->data) {
                        foreach ($result1->data as $val){
                            $listData[] = $val; //填充数据
                        }
                    }
                }
            }
        }
        return $listData;
    }

}
